package com.lanou3g.zjl.chating.home.friends;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupInfo;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.base.BaseActivity;
import com.lanou3g.zjl.chating.utils.ProgressLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * Created by dllo on 15/11/24.
 */
public class PublicGroupActivity extends BaseActivity {
    private List<EMGroupInfo> groupsList;
    private List<EMGroupInfo> data;
    private EMGroup group;
    @Bind(R.id.progress_publicGroup)
    ProgressLayout progress_publicGroup;//进度条
    @Bind(R.id.publicGroupList)
    ListView publicGroupList;
    @Override
    protected int getLayout() {
        return R.layout.public_groupactivity;
    }
    //点击查找按钮跳转到查找页面
    @OnClick(R.id.searchGroup) void searchPublicGroup(){
        Intent intent = new Intent(PublicGroupActivity.this,FindPublicGroupActivity.class);
        startActivity(intent);
    }
    @OnItemClick(R.id.publicGroupList) void gotoJoinGroup(int position){
        Intent intent = new Intent(PublicGroupActivity.this,AddPublicGroupsActivity.class);
        intent.putExtra("groupInfo",new GroupListAdapter().getItem(position));
        startActivity(intent);
        //Toast.makeText(this,new GroupListAdapter().getItem(position).getGroupName(),Toast.LENGTH_SHORT).show();
    }
    @OnClick(R.id.backToGroupList) void backToGroupList(){
        finish();
    }
    @Override
    protected void initData() {
        //显示进度条
        progress_publicGroup.showProgress();
        data = new ArrayList<>();
        //获取所有公开群列表（两种方式）
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    groupsList = EMGroupManager.getInstance().getAllPublicGroupsFromServer();//需异步处理

                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
                //过滤出公开并且不是自己建的群
                for(EMGroupInfo e : groupsList){
                    try {
                        group = EMGroupManager.getInstance().getGroupFromServer(e.getGroupId());
                    } catch (EaseMobException e1) {
                        e1.printStackTrace();
                    }
                    if(!group.getOwner().equals(EMChatManager.getInstance().getCurrentUser())&& group.isPublic()){
                        data.add(e);
                    }
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        publicGroupList.setAdapter(new GroupListAdapter());
                        progress_publicGroup.showContent();
                    }
                });

            }
        }).start();
    }
    public class GroupListAdapter extends BaseAdapter{

        @Override
        public int getCount() {

            return data != null && data.size() > 0 ? data.size() : 0;
        }

        @Override
        public EMGroupInfo getItem(int i) {

            return data != null && data.size() > 0 ? data.get(i) : null;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            EMGroupInfo info = getItem(i);
            if(view == null){
                view = LayoutInflater.from(PublicGroupActivity.this).inflate(R.layout.groups_listitem,null);
                holder = new ViewHolder(view);
                view.setTag(holder);
            }else {
                holder = (ViewHolder) view.getTag();
            }
            holder.groupName.setText(info.getGroupName());
            return view;
        }
        class ViewHolder{
            @Bind(R.id.iv)
            ImageView iv;
            @Bind(R.id.childGroupName)
            TextView groupName;
            ViewHolder(View view){

                ButterKnife.bind(this,view);
            }
        }
    }
}
